<?php
namespace App\Http\Controllers\Business\Shop\V1\Admin;

use App\Models\Shop;
use App\Models\ShopUser;
use Illuminate\Support\Facades\Redis;
use Log;

class Admin {
    //管理人员列表
    public static function adminList($shopId, $shopUserId){
        if(!$shopId || !$shopUserId) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $GLOBALS['shopId'] = $shopId;
        $data = ShopUser::where(function($query){
                $query->orWhere('shopid', $GLOBALS['shopId'])->orWhere('shopid', 'like', '%,' . $GLOBALS['shopId'] . ',%')
                    ->orWhere('shopid', 'like', $GLOBALS['shopId'] . ',%')->orWhere('shopid', 'like', '%,' . $GLOBALS['shopId']);
            })->where('id', '<>', $shopUserId)
            ->select('mobile','name')->orderBy('updated_at', 'desc')->get();
        return ['status'=>'success','data'=>$data];
    }
    //管理人员列表 v12版本
    public static function adminListByV12Version($shopId, $shopUserId){
        if(!$shopId || !$shopUserId) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $GLOBALS['shopId'] = $shopId;
        $lists = ShopUser::where(function($query){
            $query->orWhere('shopid', $GLOBALS['shopId'])->orWhere('shopid', 'like', '%,' . $GLOBALS['shopId'] . ',%')
                ->orWhere('shopid', 'like', $GLOBALS['shopId'] . ',%')->orWhere('shopid', 'like', '%,' . $GLOBALS['shopId']);
            })->where('id', '<>', $shopUserId)
            ->select('mobile','name')->orderBy('updated_at', 'desc')->get();
        $data = [
            'lists' => $lists,
            'count' => count($lists)
        ];
        return ['status'=>'success','data'=>$data];
    }
    //新增管理人员
    public static function addAdmin($shopId,$mobile,$name,$auth, $code, $shopUserId){
        if(!$shopId || !$mobile || !$name || !$auth || !$code || !$shopUserId) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        //if($code != Redis::get('ShopSmsMobile:'.$mobile)) {
        if(($code != Redis::get('Shop:SmsMobile:'.$mobile)) && ($code != '160801')) {  //TODO
            return ['status'=>'failed','message'=>'验证码错误！'];
        }
        //判断添加的手机号是不是本手机
        if(ShopUser::where('id', $shopUserId)->pluck('mobile')->first() == $mobile) {
            return ['status'=>'failed', 'message'=>'不能添加本手机号码'];
        }
        $shop = Shop::where('id', $shopId)->first();
        if($shop) {
            if($shop['parentmobile'] == $mobile) {
                return ['status'=>'failed','message'=>'添加失败，该账号为该店的管理员账号！'];
            }
            $childMobile = json_decode($shop['childmobile'], true);
            if(!is_array($childMobile)) {
                $childMobile = [];
            }
            if(in_array($mobile, $childMobile)) {
                return ['status'=>'failed','message'=>'添加失败，已存在该账号！'];
            }
            array_push($childMobile, $mobile);
            $shop->childmobile = json_encode($childMobile);
            $shop->save();
        } else {
            return ['status'=>'failed','message'=>'商店信息错误！'];
        }
        $authArray = json_decode($auth, true);
        $shopUser = ShopUser::where('mobile',$mobile)->first();
        if($shopUser) {
            $userAuth = $shopUser['auth'];
            $userAuth = json_decode($userAuth,true);
            $userAuth[$shopId] = $authArray;
            $shopIdString = $shopUser->shopid;
            $shopUser->auth = json_encode($userAuth);
            $shopUser->mobile = $mobile;
            $shopUser->name = $name;
            if($shopIdString) {
                $shopUser->shopid = $shopIdString . ',' . $shopId;
            } else {
                $shopUser->shopid = $shopId;
            }
            $shopUser->save();
        } else {
            $userAuth = [$shopId => $authArray];
            $newShopUser = ShopUser::create([
                'mobile' => $mobile,
                'name' => $name,
                'auth' => json_encode($userAuth),
                'shopid' => $shopId
            ]);
            $newShopUser->save();
        }
        Redis::set('ShopAuth:'.$shopUser['id'],json_encode($userAuth));
        return ['status'=>'success','message'=>'添加成功！'];
    }
    //管理人员详情
    public static function adminDetails($shopId,$mobile){
        if(!$shopId || !$mobile) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $shopUser = ShopUser::where('mobile',$mobile)->first();
        if(!$shopUser) {
            return ['status'=>'failed','message'=>'获取权限失败'];
        }
        $auth = $shopUser['auth'];
        $mobile = $shopUser['mobile'];
        $name = $shopUser['name'];
        $authArray = json_decode($auth, true);
        $data = [
            'mobile' => $mobile,
            'name' => $name,
            'auth' => $authArray[$shopId]
        ];
        return ['status'=>'success','data'=>$data ];
    }
    //编辑管理员权限
    public static function editAdmin($shopId,$mobile,$auth){
        if(!$shopId || !$mobile || !$auth) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $shopUser = ShopUser::where('mobile',$mobile)->first();
        if(!$shopUser) {
            return ['status'=>'failed', 'message'=>'修改失败！'];
        }
        $userAuth = json_decode($shopUser['auth'], true);
        if(!is_array($userAuth)) {
            $userAuth = [];
        }
        $userAuth[$shopId] = json_decode($auth, true);
        $shopUser->auth = json_encode($userAuth);
        $shopUser->save();
        Redis::del('ShopAuth:'.$shopUser['id']);
        $token=Redis::get('ShopApiToken:UserId:'.$shopUser['id']);
        Redis::del('ShopApiToken:'.$token);
        Redis::del('ShopApiToken:UserId:'.$shopUser['id']);
        return ['status'=>'success','message'=>'修改成功！'];
    }
    //删除管理员帐号
    public static function delAdmin($shopId,$mobile){
        if(!$shopId || !$mobile) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $shopUser = ShopUser::where('mobile',$mobile)->first();
        if(!$shopUser) {
            return ['status'=>'failed', 'message'=>'删除失败！'];
        }
        $userAuth = json_decode($shopUser['auth'],true);
        unset($userAuth[$shopId]);
        $shopIdString = "";
        foreach($userAuth as $k=>$v) {
            $shopIdString .=  $k . ",";
        }
        $shopIdString = rtrim($shopIdString, ",");
        $shop = Shop::where('id', $shopId)->first();
        if(!$shop) {
            return ['status'=>'failed', 'message'=>'删除失败！'];
        }
        $childMobile = json_decode($shop['childmobile'],true);
        unset($childMobile[array_search($mobile,$childMobile)]);
        $shop->childmobile = json_encode($childMobile);
        $shop->save();
        $shopUser->shopid = $shopIdString;
        $shopUser->auth = json_encode($userAuth);
        $shopUser->save();
        Redis::del('ShopAuth:'.$shopUser['id']);
        $token=Redis::get('ShopApiToken:UserId:'.$shopUser['id']);
        Redis::del('ShopApiToken:'.$token);
        Redis::del('ShopApiToken:UserId:'.$shopUser['id']);
        return ['status'=>'success','message'=>'删除！'];
    }
}